package com.ccsc.auth.service.impl;

import com.ccsc.auth.dao.DepartmentDao;
import com.ccsc.auth.entity.Department;
import com.ccsc.auth.service.DepartmentService;
import com.ccsc.auth.utils.DataMap;
import com.ccsc.auth.utils.Page;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yyangs on 2017/2/14.
 */
@Service
public class DepartmentServiceImpl implements DepartmentService{
    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<Department> getAllDepartments() throws Exception {
        return departmentDao.getAllDepartments();
    }

    @Override
    public List<Department> getDepartmentsByStatus(Integer status) throws Exception {
        Department department=new Department();
        department.setStatus(status);
        return departmentDao.getAllDepartmentInfosByCondition(department);
    }

    @Override
    public List<Department> getDepartmentsByName(String name) throws Exception {
        Department department=new Department();
        department.setName(name);
        return departmentDao.getAllDepartmentInfosByCondition(department);
    }

    @Override
    public List<Department> getDepartmentsByParentDepartmentId(Integer parentDepartmentId) throws Exception {
        Department department=new Department();
        Department parentDepartment=new Department();
        parentDepartment.setId(parentDepartmentId);
        department.setParentDepartment(parentDepartment);
        List<Department> departmentList=departmentDao.getAllDepartmentInfosByCondition(department);
        //循环查询
        departmentList.addAll(getDepartmemntByParentId(departmentList));
        //将自己添加进list
        department.setId(parentDepartmentId);
        department=departmentDao.getDepartmentInfoByIdOrIdentifiers(department);
        departmentList.add(department);
        return  departmentList;
    }


    public List<Department> getDepartmemntByParentId(List<Department> departments) throws  Exception{
        List<Department> departmentList=new ArrayList<>();
        for (Department department:departments) {
            Department departmentTemp=new Department();
            Department parentDepartment=new Department();
            parentDepartment.setId(department.getId());
            departmentTemp.setParentDepartment(parentDepartment);
            departmentList=departmentDao.getAllDepartmentInfosByCondition(departmentTemp);
            departmentList.addAll(getDepartmemntByParentId(departmentList));
        }
        return departmentList;
    }

    @Override
    public Page getDepartmentsByPage(DataMap params) throws Exception {
        List<Department> resultList = departmentDao.getAllDepartments(new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Department> pageList = (PageList<Department>) resultList;
        return new Page(pageList);
    }

    @Override
    public Page getDepartmentsByPage(Department department, DataMap params) throws Exception {
        List<Department> resultList = departmentDao.getAllDepartmentInfosByCondition(department,new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Department> pageList = (PageList<Department>) resultList;
        return new Page(pageList);
    }

    @Override
    public Department getDepartmentById(Integer id) throws Exception {
        Department department=new Department();
        department.setId(id);
        return departmentDao.getDepartmentInfoByIdOrIdentifiers(department);
    }

    @Override
    public Department getDepartmentByIdentifier(String identifier) throws Exception {
        Department department=new Department();
        department.setIdentifier(identifier);
        return departmentDao.getDepartmentInfoByIdOrIdentifiers(department);
    }

    @Override
    public Integer createDepartment(Department department) throws Exception {
        return departmentDao.createDepartment(department);
    }

    @Override
    public Integer updateDepartment(Department department) throws Exception {
        return departmentDao.updateDepartment(department);
    }

    @Override
    public Integer deleteDepartmentById(Integer id) throws Exception {
        Department department=new Department();
        department.setId(id);
        return departmentDao.deleteDepartment(department);
    }

    @Override
    public Integer activeDepartment(Integer id) throws Exception {
        Department department=new Department();
        department.setId(id);
        department.setStatus(1);
        return departmentDao.updateDepartment(department);
    }

    @Override
    public Integer withdrawDepartment(Integer id) throws Exception {
        Department department=new Department();
        department.setId(id);
        department.setStatus(0);
        return departmentDao.updateDepartment(department);
    }

    @Override
    public Integer batchActiveDepartment(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=activeDepartment(id);
        }
        return result;
    }

    @Override
    public Integer batchWithdrawDepartment(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=withdrawDepartment(id);
        }
        return result;
    }
}
